package com.sczfdf.www.sda.gov.cn.pipeline;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;

import com.sczfdf.www.sda.gov.cn.dao.C3P0ConnentionProvider;
import com.sczfdf.www.sda.gov.cn.modo.GovImageDO;

import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.PageModelPipeline;

public class GovImagePipeline implements PageModelPipeline<GovImageDO> {

	//获取数据库连接池
	private static String SQL = "INSERT INTO `webmagic_yaojianju_page_image` (`KEY`, image)  VALUES (?, ?);";

	private PreparedStatement ps = null;
	private Connection conn = null;

	LocalDateTime now;
	@Override
	public synchronized void process(GovImageDO g, Task task) {

		try {
			conn = C3P0ConnentionProvider.getConnection();
			ps = conn.prepareStatement(SQL);

			/**
			 * 补完
			 */
			ps.setString(1, g.getKEY());
			ps.setString(2, g.getAll());

			ps.executeUpdate();

		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			connClose(conn,ps);
		}
		return;
	}

	/**
	 * 关闭链接
	 * @param conn 数据库链接
	 * @param ps 预编译语句
	 * @param reSet 结果集
	 */
	public void connClose(Connection conn,PreparedStatement ps ,ResultSet reSet){
		try {
			if(ps != null){
				ps.close();
			}
			if(reSet != null){
				reSet.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			connClose(conn, ps);
		}
	}

	/**
	 * 关闭链接
	 * @param conn 数据库链接
	 * @param ps 预编译语句
	 */
	public void connClose(Connection conn,PreparedStatement ps){
		if(ps != null){
			try {
				ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(conn!=null){
			try {  
				conn.close();  
			} catch (SQLException e) {  
				e.printStackTrace();  
			}  
		}  
	}
}
